<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:12
 */

namespace App\Http\Services;


use App\Http\Model\Guard;
use App\Http\Model\Hint;

class RoomMicroRadioServices extends RoomMicroServices
{

    protected $modelGuard;
    public function __construct()
    {
        $this->modelGuard = new Guard();
        parent::__construct();
    }


    public function get_micros($room_id)
    {
        $micros = parent::get_micros($room_id);
        foreach ($micros as $k => $v){
            $micros[$k]['guard'] = $this->getMicroGuardType($v['microphone_id']);
        }
        return $micros;
    }


    public function getMicroGuardType($microId)
    {
        $guardType = 0;
        if ($microId == 0){
            $guardType = 3;
        }
        if ($microId == 1 || $microId == 2 || $microId == 3 ){
            $guardType = 2;
        }
        if ($microId == 4 || $microId == 5 || $microId == 6 || $microId == 7 ){
            $guardType = 1;
        }
        return $guardType;
    }

    public function hostMicroOut($roomId)
    {
        $microArr = $this->model_room_micro->get_micros($roomId, 0);
        $retData = [];
        foreach ($microArr as $micro){
            if (empty($micro['user_id'])){
                continue;
            }
            $retData[] = $micro;
        }
        return $retData;
        //return parent::hostMicroOut(); // TODO: Change the autogenerated stub
    }

    public function into_req($room_id, $micro_id, $user_id)
    {
        $guardType = $this->getMicroGuardType($micro_id);
        $hostMicroUserId = $this->model_room_hostmicro->get_user_id($room_id);
        if (empty($hostMicroUserId)){
            return Hint::ROOM_HOSTMICRO_IS_NULL; //主持麦无人，无法上麦
        }
        $curUser = $this->model_user->get_current_user();
        if (empty($curUser)){
            return Hint::LOGIN_OUT;
        }
        if ($curUser['user_id'] != $hostMicroUserId){
            return Hint::AUTH_ERROR;
        }
        $hasGuard = $this->modelGuard->hasGuard($user_id, $hostMicroUserId, $guardType);
        if (!$hasGuard){
            return Hint::ROOM_MICRO_GUARD_MATCH; //没有麦守护类型
        }
        return parent::into_req($room_id, $micro_id, $user_id);
    }

    public function into($room_id, $micro_id, &$user_id)
    {
        $guardType = $this->getMicroGuardType($micro_id);
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        $hostMicroUserId = $this->model_room_hostmicro->get_user_id($room_id);
        if (empty($hostMicroUserId)){
            return Hint::ROOM_HOSTMICRO_IS_NULL; //主持麦无人，无法上麦
        }
        $hasGuard = $this->modelGuard->hasGuard($user_id, $hostMicroUserId, $guardType);
        if (!$hasGuard){
            return Hint::ROOM_MICRO_GUARD_MATCH; //没有麦守护类型
        }
        return parent::into($room_id, $micro_id, $user_id);
    }

    public function jump($room_id, $jump_micro_id, &$micro_id, &$user_id, &$volume, &$volume_self)
    {
        $guardType = $this->getMicroGuardType($jump_micro_id);
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        $hostMicroUserId = $this->model_room_hostmicro->get_user_id($room_id);
        if (empty($hostMicroUserId)){
            return Hint::ROOM_HOSTMICRO_IS_NULL; //主持麦无人，无法上麦
        }

        $hasGuard = $this->modelGuard->hasGuard($user_id, $hostMicroUserId, $guardType);
        if (!$hasGuard){
            return Hint::ROOM_MICRO_GUARD_MATCH; //没有麦守护类型
        }
        return parent::jump($room_id, $jump_micro_id, $micro_id, $user_id, $volume, $volume_self); // TODO: Change the autogenerated stub
    }
}